﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LIM.controller;
using LIM.model;
using Telerik.WinControls.RichTextBox.FormatProviders.Txt;

namespace LIM.forms.uc_DanhMuc_ChiTieu
{
    public partial class uc_DanhMuc_ChiTieu_PhuongPhap : UserControl
    {
        DanhMucChiTieuController controller = new DanhMucChiTieuController();
        public uc_DanhMuc_ChiTieu_PhuongPhap()
        {
            InitializeComponent();
        }
        radForm_DanhMuc_ChiTieu parentForm = null;
        void parentForm_OnRowChange()
        {
            if (parentForm.SelectedEntity != null)
            {
                LoadData();
            }
        }
        void LoadData()
        {
            if (parentForm.SelectedEntity.RowID != 0)
            {
                this.radRichTextBox1.Document.Selection.SelectAll();
                this.radRichTextBox1.Document.Delete(false);
                radRichTextBox1.Document.Insert(controller.LoadByID(parentForm.SelectedEntity.RowID).ChiTietPhuongPhap, radRichTextBox1.Document.Style);
            }
        }

        private void uc_DanhMuc_ChiTieu_PhuongPhap_Load(object sender, EventArgs e)
        {
            parentForm = (radForm_DanhMuc_ChiTieu)this.ParentForm;
            if (parentForm != null)
            {
                parentForm.OnRowChange += new radForm_DanhMuc_ChiTieu.GridViewDoubleClick(parentForm_OnRowChange);
            }
            if (parentForm.SelectedEntity != null)
            {
                LoadData();
            }
        }

        private void radMenuItem7_Click(object sender, EventArgs e)
        {
            if (parentForm.SelectedEntity != null)
            {
                string _mess = "";
                int _code = 0;
                tbl_DanhMuc_ChiTieu item = parentForm.controller.LoadByID(parentForm.SelectedEntity.RowID);
                var provider = new TxtFormatProvider();
                item.ChiTietPhuongPhap = provider.Export(radRichTextBox1.Document);
                if (parentForm.controller.SaveObject(item, out _mess, out _code))
                {
                    RadDesktopAlert_SavingStatus.ShowAlertSaveSuccess("Phương pháp thực hiện", "Đã lưu thành công!");
                    parentForm.RefreshData();
                }
                else
                    MessageBox.Show(_mess);
            }
        }
    }
}
